<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class OxTarget 
{
    public $idtargets;
    public $name;
    private $geoscope;
    private $locations;
    private $tags;
    private $people;
    
    /* @var $geostamps OxGeostamp */
    private $target_geostamps;
    /* @var $tags OxTag */
    private $target_tags;
    /* @var $people OxPeople */
    private $target_people;
    
    function __construct()
    {
        $new_target->target_geostamps = array();
        $new_target->target_tags = array();
        $new_target->target_people = array();        
    }
    
    public static function from_id($id)
    {
        $CI =& get_instance();
        
		$query = $CI->db->select('*')
                              ->from('targets')
                              ->where('idtargets',$id)
                              ->get()->result();
        
        return OxTarget::from_data($query[0]);
    }
    public static function from_user_id($user_id)
    {
        $CI =& get_instance();
        
		$query = $CI->db->select('targets.*')
                              ->from('users_targets')
                              ->join('targets', 'users_targets.idtarget = targets.idtargets')
                              ->where('users_targets.iduser',$user_id)
                              ->order_by("priority", "asc")
                              ->get()->result();
        $res = array();
        foreach($query as $q)
        {
            $res[] = OxTarget::from_data($q);
        }
        
        return $res;
    }
    
    private static function from_data($data)
    {
        $new_target = new OxTarget();
        $new_target->idtargets = $data->idtargets;
        $new_target->name = $data->name;
        $new_target->geoscope = $data->geoscope;
        $new_target->locations = $data->locations;
        $new_target->tags = $data->tags;
        $new_target->people = $data->people;
        
        $new_target->target_geostamps = array();
        $new_target->target_tags = array();
        $new_target->target_people = array();
        return $new_target;
    }
    
    public function GeoStamps()
    {
        if($this->locations && !$this->target_geostamps)
        {
            $this->target_geostamps = OxGeostamp::fromids($this->locations);
        }
        return $this->target_geostamps;
    }
    public function Tags()
    {
        if($this->tags && !$this->target_tags)
        {
            $this->target_tags = OxTag::from_ids($this->tags);
        }
        return $this->target_tags;
    }
    public function People()
    {
        if($this->people && !$this->target_people)
        {
            $this->target_people = OxPeople::from_ids($this->people);
        }
        return $this->target_people;
    }
}